package com.hyacinth.core.sysmgr.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hyacinth.common.constant.PersistResult;
import com.hyacinth.common.framework.ControllerSupport;
import com.hyacinth.common.framework.i.IServiceSupport;
import com.hyacinth.core.sysmgr.domain.SysMenu;
import com.hyacinth.core.sysmgr.domain.SysRole;
import com.hyacinth.core.sysmgr.service.IMenuService;
import com.hyacinth.core.sysmgr.service.IRoleService;
import com.hyacinth.web.ui.easyui.TreeFormatter;

@Controller
@RequestMapping("/system/role/*")
public class RoleController extends ControllerSupport<SysRole>{

	private static final long serialVersionUID = -8717049011814854261L;
	
	private IRoleService roleService;
	
	private IMenuService menuService;
	
	@RequestMapping("enter.do")
	public String enter() throws Exception {
		return "/system/role/list";
	}
	
	@RequestMapping("edit.do")
	public ModelAndView edit(@RequestParam Long id) throws Exception {
		if(id!=null) {
			Map<String,SysRole> map = new HashMap<String, SysRole>();
			SysRole m = roleService.find(id);
			map.put("domain", m);
			return new ModelAndView("/system/role/edit",map);
		}
		return new ModelAndView("/system/role/edit");
	}
	
	/**
	 * 编辑权限的页面
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("editPower.do")
	public ModelAndView editPower(@RequestParam Long id) throws Exception {
		Map<String,Object> map = new HashMap<String, Object>();
		if(id != null) {
			map.put("roleId", id);
			return new ModelAndView("/system/role/editPower",map);
		}
		return new ModelAndView("/system/role/editPower");
	}
	
	@RequestMapping("getPowerMenu.do")
	public @ResponseBody String getPowerMenu(@RequestParam Long roleId) throws Exception {
		if(roleId!=null) {
			SysRole role = roleService.find(roleId);
			List<SysMenu> allMenus = menuService.all();
			List<SysMenu> roleMenus = role.getMenus();
			return new TreeFormatter().getMenuData(allMenus, roleMenus);
		}
		return "[]";
	}
	
	@RequestMapping("updateMenuPower.do")
	public @ResponseBody String updateMenuPower(@RequestParam Long menuId, @RequestParam Long roleId, @RequestParam boolean checked) throws Exception {
		try {
			SysMenu menu = menuService.find(menuId);
			SysRole role = roleService.find(roleId);
			if(checked)
				role.getMenus().add(menu);
			else
				role.getMenus().remove(menu);
			roleService.merge(role);
		}catch(Exception e) {
			e.printStackTrace();
			return printOut(PersistResult.ERROR, PersistResult.MSG_ERROR);
		}
		return printOut(PersistResult.SUCCESS, PersistResult.MSG_SUCCESS);
	}

	@Override
	public IServiceSupport<SysRole> getService() {
		return roleService;
	}

	@Override
	public String[] getFields() {
		String[] fields = {"name","alias","id"};
		return fields;
	}

	public IRoleService getRoleService() {
		return roleService;
	}

	@Resource
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}

	public IMenuService getMenuService() {
		return menuService;
	}

	@Resource
	public void setMenuService(IMenuService menuService) {
		this.menuService = menuService;
	}
}
